import { createRouter, createWebHistory } from 'vue-router';
import HomePage from '@/views/Home.vue';
import CoursePage from '@/views/Course.vue';
import LoginPage from '@/views/Login.vue';
import RegisterPage from '@/views/Register.vue';
import CourseResourcePage from '@/views/CourseResource.vue';
import VideoPlayerPage from "@/views/VideoPlayer";
import AssignmentsPage from '@/views/Assignments.vue'; 
import SurveyStatisticsPage from '@/views/SurveyStatistics.vue';
import SurveyAll from '@/views/survey/SurveyAll.vue';
import SurveySubmitted from '@/views/survey/SurveySubmitted.vue';
import SurveyNotSubmitted from '@/views/survey/SurveyNotSubmitted.vue';
import SurveyDetail from '@/views/survey/SurveyDetail.vue';
import ProfilePage from '@/views/Profile.vue';
import CourseIntro from '@/components/CourseIntro.vue'; 
import QuestionsPage from '@/views/Questions.vue'; 
import QuestionDetailPage from '@/views/QuestionDetail.vue';
import PublishTopicPage from '@/views/PublishTopic.vue';
import AISearchPage from '@/views/AISearch.vue'; 
import HomeSearch from '@/views/HomeSearch.vue';
import AssignmentDetailsPage from '@/views/AssignmentDetails.vue'; 
import MyCoursesPage from '@/views/MyCourses.vue';
import SemesterIntro from '@/components/SemesterIntro.vue';
import knowledge from '@/views/knowledge.vue';
import BeforeCourseIntro from '@/components/BeforeCourseIntro.vue';
import BeforeAISearchPage from '@/views/BeforeAISearch .vue';
import beforeknowledge from '@/views/beforeknowledge.vue';
import PeerReviewPage from '@/views/PeerReview.vue';
import help from '@/views/help.vue';
import beforehelp from '@/views/beforehelp.vue';
import ChapterTestPage from '@/views/ChapterTest.vue';

const routes = [
  { path: '/', name: 'Home', component: HomePage },
  { path: '/courses', name: 'Course', component: CoursePage },
  { path: '/login', name: 'Login', component: LoginPage },
  { path: '/register', name: 'Register', component: RegisterPage },
  { path: '/course-resource', name: 'CourseResource', component: CourseResourcePage },
  { path: '/video/:videoId', name: 'VideoPlayer', component: VideoPlayerPage,
    props: (route) => ({
      initialResourceId: route.query.resourceId ? parseInt(route.query.resourceId) : null,
    }),
  },
  { path: '/assignments', name: 'Assignments', component: AssignmentsPage },
  { path: '/survey', name: 'SurveyStatistics', component: SurveyStatisticsPage, redirect: '/survey/surveyall', children: [
    { path: 'surveyall', component: SurveyAll },
    { path: 'surveysubmitted', component: SurveySubmitted },
    { path: 'surveynot-submitted', component: SurveyNotSubmitted }
  ]},
  { path: '/survey/:id', name: 'SurveyDetail', component: SurveyDetail },
  { path: '/profile', name: 'Profile', component: ProfilePage },
  { path: '/course-intro', name: 'CourseIntro', component: CourseIntro },
  { path: '/semester-intro', name: 'SemesterIntro', component: SemesterIntro },
  { path: '/knowledge', name: 'knowledge', component: knowledge },
  { path: '/questions', name: 'Questions', component: QuestionsPage },
  { path: '/questions/question-detail/:id', name: 'QuestionDetail', component: QuestionDetailPage, props: true, beforeEnter: (to, from, next) => {
    next(vm => {
      vm.fromPath = from.fullPath;
      vm.fromTab = from.query.tab || 'latest';
    });
  }},
  { path: '/questions/publish-topic', name: 'PublishTopic', component: PublishTopicPage },
  { path: '/AI-search', name: 'AISearch', component: AISearchPage },
  { path: '/homesearch', name: 'HomeSearch', component: HomeSearch },
  { path: '/assignment/:id', name: 'AssignmentDetails', component: AssignmentDetailsPage },
  { path: '/mycourses', name: 'MyCourses', component: MyCoursesPage },
  { path: '/before-course-intro', name: 'BeforeCourseIntro', component: BeforeCourseIntro },
  { path: '/before-ai-search', name: 'BeforeAISearch', component: BeforeAISearchPage },
  { path: '/before-knowledge', name: 'beforeknowledge', component: beforeknowledge },
  { path: '/help', name: 'help', component: help },
  { path: '/beforehelp', name: 'beforehelp', component: beforehelp },
  { path: '/peer-review', name: 'PeerReview', component: PeerReviewPage },
  { path: '/chapter-test/:test_id', name: 'ChapterTest', component: ChapterTestPage, props: true }
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;
